package com.jinsi.controller.yrf;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jinsi.constant.YrfConstant;
import com.jinsi.pojo.*;

import com.jinsi.service.yrf.BackgroundmanagementpersonnellistService;
import com.jinsi.service.yrf.OperationLogService;
import com.jinsi.service.yrf.SealLogService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author: 86185
 * @description: 后台管理人员
 * @date 2022/9/11
 */
@RestController
@RequestMapping("/backgroundmanagementpersonnellist")
@Slf4j
public class BackGroundManagementPersonnelListController {

    @Autowired
    private SealLogService sealLogService;
    @Autowired
    private BackgroundmanagementpersonnellistService baclistservice;

    @Autowired
    private OperationLogService operationLogService;

    @Autowired
    private FastFileStorageClient client;

    /**
     * 获取树的数据
     * @return 返回数据
     */
    @RequestMapping("/tree")
    public List<Department> getTree(){
        return baclistservice.getTree();
    }

    /**
     * 获取表格数据
     * @param aa 模糊查
     * @param limit 分页
     * @param deptName 模糊查
     * @param userAccount 模糊查
     * @param userNickname 模糊查
     * @param userCreateDate 根据userId查询
     */
    @RequestMapping("/getAll")
    public R getAll(Integer page, Integer limit, Integer aa, String userAccount, String userNickname, String deptName, Date userCreateDate){
        PageHelper.startPage(page,limit);
        List<SyUser> all = baclistservice.getAll(aa,userAccount,userNickname,deptName,userCreateDate);
        PageInfo<SyUser> pageInfo = new PageInfo<>(all);
        return R.custom(YrfConstant.LAYUI_CODE,
                YrfConstant.LAYUI_MSG,
                pageInfo.getList(),
                pageInfo.getTotal());

    }

    /**
     * 后台管理人员列表是否启用
     * @param userId 根据id
     * @param userEnableStatus 修改的状态
     * @return 返回状态码
     */
    @RequestMapping("/updateStatus")
    public R updatedeptEnableStatus(int userId,int userEnableStatus){
        baclistservice.updatedeptEnableStatus(userId,userEnableStatus);

        return R.ok("成功");

    }

    /**
     *停封用户
     * @param userId 根据userid
     * @param userStats 状态
     * @return 状态码
     */
    @RequestMapping("/updateStats")
    public R updatedeptuserStats(int userId, int userStats, HttpServletRequest servletRequest){
        R re = null;
        baclistservice.updatedeptuserStats(userId,userStats);
        if(userStats == 2){
            SySealLog aa = new SySealLog();
            SyUser syUser = (SyUser) servletRequest.getSession().getAttribute("user");
            if (syUser != null) {
                aa.setSealAdministratorName(syUser.getUserName());
                List<SyUser> see = baclistservice.see(userId);
                aa.setSealUserAccount(see.get(0).getUserAccount());
                aa.setSealUserNickname(see.get(0).getUserNickname());
                aa.setSealLogType("封停");
                aa.setSealExecutionTime(new Date());
                sealLogService.sySealLogadd(aa);
                re = R.ok("停封成功了");
            }

        }else {
            re = R.ok( "状态修改成功");
        }

        return re;

    }

    /**
     * 后台管理人员列表查看
     * @param userId 根据id
     * @return 返回状态数据
     */
    @RequestMapping("/see")
    public R see(int userId){

        List<SyUser> all = baclistservice.see(userId);
        try {
            operationLogService.addlog("后台管理人员列表","查看","查看东西");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加操作日志error --->",e);
        }

        return R.custom(200,"成功",all);

    }


    /**
     * 后台管理人员列表编辑
     *
     * @param userId 根据id
     * @param userNickname
     * @param userUrl
     * @param userSex
     * @param userPhone
     * @param userIdCard
     * @param userDateBirth
     * @param deptName
     * @param userEmail
     * @return 状态码
     */
    @RequestMapping("/syuseredit")
    public R syuserbj(Integer userId, @Range(min = 1,max = 6,message = "输入的用户昵称不合法") String userNickname,
                      String userUrl, int userSex,
                      @Pattern(regexp = "/^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$/", message = "手机号格式错误！") String userPhone,
                      @Pattern(regexp = "/^([1-6][1-9]|50)\\d{4}(18|19|20)\\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$/", message = "不符合18位身份证！") String userIdCard,
                      Date userDateBirth, @NotBlank(message = "部门名称不能为空！") String deptName,
                      @Email String userEmail){
        R re = null;
        try {
            baclistservice.syuserbj(userId,userNickname,userUrl,userSex,userPhone,userIdCard,userDateBirth,deptName,userEmail);
            operationLogService.addlog("后台管理人员列表","编辑","修改东西");
            re = R.ok("成功");
        } catch (Exception e) {
            re = R.error("失败");
            e.printStackTrace();
            log.error("添加操作日志或编辑人员列表error --->",e);
        }


        return re;

    }


    /**
     * 封停用户天数
     * @param syUserSeal 要停封的用户
     */
    @RequestMapping("/syuserbjft")
    public R syuserbjft(@Valid SyUserSeal syUserSeal){

        R re = null;
        SyUserSeal ck = baclistservice.syuserckft(syUserSeal.getUserId());
        log.info("将要停封用户id为: --->"+ck.getUserId());
        if(ck == null){
            try {
                baclistservice.syuserbjft(syUserSeal);
                re = R.ok("该用户不存在已经添加封停");
            } catch (Exception e) {
                re = R.error("失败");
                e.printStackTrace();
                log.error("添加封停用户error --->",e);
            }
        }else {
            try {
                syUserSeal.setSealId(ck.getSealId());
                System.out.println("前端传入数据"+ syUserSeal);
                baclistservice.syuserbjft2(syUserSeal);
                re = R.ok("该用户存在已经追加");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("追加的封停用户error为: --->",e);
            }
        }

        return re;

    }

}
